﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NCommet.Core;
using NCommet.Core.Configuration;
using NCommet.Core.Agents;
using NCommet.Core.Services;

namespace NCommet.TestGUI
{
    public partial class NodePropertiesForm : Form
    {
        public NodePropertiesForm()
        {
            InitializeComponent();
        }

        private Item item;

        public void LoadItem(Item item)
        {
            IWhoAmI iwhoami = NCommetContainer.Instance.WhoAmI;
            if (iwhoami != null)
            {
                foreach (string role in iwhoami.GetAvailableRoles())
                    lstRoles.Items.Add(role);
            }
            lblGenericID.Text = item.Id.ToString();
            this.item = item;    
            
        }

        private void LoadAccessLevel(AllowDenyControl ctl, AccessLevel accessLevelRequired,
            AccessLevel accessLevelPresent)
        {
                ctl.Value = (accessLevelPresent & accessLevelRequired)==accessLevelRequired ? AllowDenyValue.Allow : AllowDenyValue.Deny;
            
        }

        private void lstRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            IAuthorizationManager authman = NCommetContainer.Instance.AuthorizationManager;
            AccessLevel accessLevel;
            if (authman != null)
            {
                accessLevel = authman.GetAccessLevel(item, lstRoles.SelectedItem.ToString());
                bool inherited = !authman.IsAccessLevelSet(item, lstRoles.SelectedItem.ToString());
                chkInherit.Checked = inherited;
                LoadAccessLevel(adView, AccessLevel.View, accessLevel);
                LoadAccessLevel(adEdit, AccessLevel.Edit, accessLevel);
                LoadAccessLevel(adDelete, AccessLevel.Delete, accessLevel);
                LoadAccessLevel(adDetach, AccessLevel.Detach, accessLevel);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            IAuthorizationManager authman = NCommetContainer.Instance.AuthorizationManager;
            if (authman != null)
            {
                if (chkInherit.Checked)
                {
                    authman.ResetAccessLevel(item, lstRoles.SelectedItem.ToString());
                }
                else
                {
                    AccessLevel result = AccessLevel.None;
                    if (adView.Value == AllowDenyValue.Allow)
                        result |= AccessLevel.View;
                    if (adEdit.Value == AllowDenyValue.Allow)
                        result |= AccessLevel.Edit;
                    if (adDelete.Value == AllowDenyValue.Allow)
                        result |= AccessLevel.Delete;
                    if (adDetach.Value == AllowDenyValue.Allow)
                        result |= AccessLevel.Detach;
                    authman.SetAccessLevel(item, lstRoles.SelectedItem.ToString(), result);
                }
            }
            Hide();
        }
    }
}